package study;

import org.jboss.resteasy.cdi.Utils;
import org.jboss.resteasy.spi.*;

import javax.ws.rs.WebApplicationException;

/**
 * Created by 徐翔 on 14-3-11.
 */
public class GuicePropertyInject implements PropertyInjector{
    private PropertyInjector delegate;
    private Class<?> clazz;
    private boolean injectorEnabled = true;

    public GuicePropertyInject(PropertyInjector delegate, Class<?> clazz,Jedi jedi){
        this.delegate = delegate;
        this.clazz = clazz;
        if(jedi.getInjector().getAllBindings().containsKey(clazz) && Utils.isJaxrsComponent(clazz)){
            injectorEnabled = false;
        }
    }


    @Override
    public void inject(Object target) {
        if (injectorEnabled)
        {
            delegate.inject(target);
        }
    }

    @Override
    public void inject(HttpRequest request, HttpResponse response, Object target) throws Failure, WebApplicationException, ApplicationException {
        if (injectorEnabled)
        {
            delegate.inject(request, response, target);
        }
    }

    @Override
    public String toString()
    {
        return "GuicePropertyInject (enabled: " + injectorEnabled + ") for " + clazz;
    }
}
